﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ThuPhap.Class;
using System.Linq;

namespace ThuPhap
{
    public partial class InputMessageBoxControl : PhoneApplicationPage
    {
        private string[] filenames;
        public delegate void FinishedInputNameDelegate(string FileName, bool IsReplace = false);
        public static event FinishedInputNameDelegate FinishedInputNameEvent;
        public InputMessageBoxControl()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string result = string.Empty;
            SavePictureInfo saveItem = new SavePictureInfo();
            filenames = saveItem.GetFileNames();
            int i = 0;
            List<string> files = new List<string>();
            for (i = 0; i < filenames.Length; i++)
            {
                if (filenames[i].StartsWith("ThuPhap") == true)
                {
                    try
                    {
                        string number = filenames[i].Replace(".tp", "").Substring(7);
                        int index = int.Parse(number);
                        break;
                    }
                    catch { }
                }
            }
            
            //var files = from q in filenames
            //            where q.StartsWith("ThuPhap") == true
            //            select q;
            if (i==filenames.Length)
            {
                result = "ThuPhap1";
            }
            else
            {
                string[] list = filenames;
                if (list[i].Length >= 11)
                {
                    
                    result = "ThuPhap1";
                    int indexList = i;
                    while (indexList<list.Length)
                    {
                        try
                        {
                            //string number = list[indexList].Substring(7, 1);
                            string number = list[indexList].Replace(".tp", "").Substring(7);
                            int index = int.Parse(number);
                            result = "ThuPhap" + ++index;
                            if (indexList < list.Length)
                            {
                                var filenameTemp = result + ".tp";
                                var isDuplicate = list.Single(o => o == filenameTemp);
                                if (isDuplicate == null)
                                {
                                    break;
                                }
                                //else
                                //{
                                //    result = "ThuPhap" + ++index;
                                //}

                                
                            }
                            
                        }
                        catch
                        {
                            //result = "ThuPhap1";
                           // break;
                        }
                        indexList++;
                    }
                    
                }
                else
                {
                    result = "ThuPhap1";
                }
            }

            this.FileNameTextBox.Text = result;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            string filename = this.FileNameTextBox.Text;
            if (filenames.Length == 0)
            {
                if (FinishedInputNameEvent!=null)
                {
                    FinishedInputNameEvent(filename);
                    NavigationService.GoBack();
                }
            }
            else
            {
                if (this.CheckDuplicateFileName(filename))
                {
                    var result = MessageBox.Show(string.Format("{0} already exists.\nDo you want to replace it?", filename), "Notice", MessageBoxButton.OKCancel);
                    if (result == MessageBoxResult.OK)
                    {
                        if (FinishedInputNameEvent != null)
                        {
                            FinishedInputNameEvent(filename, true);
                        }
                        NavigationService.GoBack();
                    }
                }
                else
                {
                    if (FinishedInputNameEvent != null)
                    {
                        FinishedInputNameEvent(filename);
                        NavigationService.GoBack();
                    }
                }
            }
        }

        private bool CheckDuplicateFileName(string FileName)
        {
            FileName = FileName + ".tp";
            foreach (var item in filenames)
            {
                if (FileName == item)
                {
                    return true;
                }
            }
            return false;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void FileNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (FileNameTextBox.Text!=string.Empty)
            {
                OKButton.IsEnabled = true;
            }
            else
            {
                OKButton.IsEnabled = true;
            }
        }
    }
}